from pydantic import BaseModel, Field, IPvAnyAddress
from typing import Optional, List
from datetime import datetime
from backend.models import DeviceType, BackupStatus

# Device Schemas
class DeviceBase(BaseModel):
    name: str = Field(..., min_length=1, max_length=100)
    hostname: str = Field(..., min_length=1, max_length=255)
    ip_address: str
    device_type: str
    port: int = Field(default=22, ge=1, le=65535)
    username: str
    password: str
    enable_password: Optional[str] = None
    description: Optional[str] = None
    is_active: bool = True
    auto_backup: bool = True
    backup_interval: int = Field(default=24, ge=1)

class DeviceCreate(DeviceBase):
    pass

class DeviceUpdate(BaseModel):
    name: Optional[str] = None
    hostname: Optional[str] = None
    ip_address: Optional[str] = None
    device_type: Optional[str] = None
    port: Optional[int] = None
    username: Optional[str] = None
    password: Optional[str] = None
    enable_password: Optional[str] = None
    description: Optional[str] = None
    is_active: Optional[bool] = None
    auto_backup: Optional[bool] = None
    backup_interval: Optional[int] = None

class DeviceResponse(BaseModel):
    id: int
    name: str
    hostname: str
    ip_address: str
    device_type: str
    port: int
    username: str
    description: Optional[str]
    is_active: bool
    auto_backup: bool
    backup_interval: int
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True

# Backup Schemas
class BackupResponse(BaseModel):
    id: int
    device_id: int
    filename: str
    file_size: int
    status: str
    error_message: Optional[str]
    backup_time: datetime
    is_manual: bool
    config_hash: Optional[str]
    
    class Config:
        from_attributes = True

class BackupWithDevice(BackupResponse):
    device: DeviceResponse

# Schedule Log Schemas
class ScheduleLogResponse(BaseModel):
    id: int
    job_name: str
    status: str
    message: Optional[str]
    devices_count: int
    success_count: int
    failed_count: int
    execute_time: datetime
    
    class Config:
        from_attributes = True

# Statistics
class Statistics(BaseModel):
    total_devices: int
    active_devices: int
    total_backups: int
    today_backups: int
    success_rate: float
    total_backup_size: int

